package org.lpw.wormhole.scheduler;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 分钟定时器。用于发起定时任务。
 * 
 * @author lpw
 */
public class MinuteTimer
    implements Runnable
{
    private final List<MinuteJob> jobs;
    private boolean runnable;

    public MinuteTimer()
    {
        super();

        jobs = Collections.synchronizedList(new ArrayList<MinuteJob>());
        runnable = true;
    }

    public void addMinuteJob(MinuteJob minuteJob)
    {
        jobs.add(minuteJob);
    }

    @Override
    public void run()
    {
        if(jobs.isEmpty())
            return;

        try
        {
            for(; runnable; Thread.sleep(60000L))
                for(MinuteJob job : jobs)
                    job.executeInMinute();
        }
        catch(InterruptedException e)
        {
        }
    }

    public void exit()
    {
        runnable = false;
    }
}
